<!--
**********************************************************
* @Author: 张小平
* @Date:   2024-05-01
* @Email:  qqshuqian@163.com
* @Last modified: 最后修改日期
* @Description: 文件描述
**********************************************************
-->
<template>
  <basic-container>
    <div class="avue-crud">
      <el-form :inline="true">
        <el-form-item><el-button v-if="ckeckCommPermission('addBtn')" icon="el-icon-plus" type="primary" @click="addOrUpdateHandle(false,null)">添加</el-button></el-form-item>
      </el-form>

      <el-table border v-loading="loading" :data="menuList" row-key="id" :tree-props="{children: 'children', hasChildren: 'hasChildrens'}">
        <el-table-column prop="name" label="菜单名称" :show-overflow-tooltip="true" width="180"></el-table-column>
        <el-table-column prop="icon" label="图标" align="center" width="60">
          <template #="scope">
            <i :class="scope.row.icon" />
          </template>
        </el-table-column>
        <el-table-column prop="sort" label="排序" width="60"></el-table-column>
        <el-table-column prop="path" label="路由" :show-overflow-tooltip="true"></el-table-column>

        <el-table-column prop="component" label="组件" :show-overflow-tooltip="true"></el-table-column>

        <el-table-column prop="type" label="类型" width="90" align="center">
          <template #="scope">
            <el-tag type="success" v-if="scope.row.type == '0'">菜单</el-tag>
            <el-tag type="success" v-if="scope.row.type == '2'">接口</el-tag>
            <el-tag type="info" v-if="scope.row.type == '1'">按钮</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="hide" label="隐藏" width="80" align="center">
          <template #="scope">
            <el-tag type="info" v-if="scope.row.hide == '1'">是</el-tag>
            <el-tag type="success" v-if="scope.row.hide == '0'">否</el-tag>
          </template>
        </el-table-column>
          <el-table-column prop="pub" label="公共" width="80" align="center">
            <template #="scope">
              <el-tag type="info" v-if="scope.row.pub == '1'">是</el-tag>
              <el-tag type="success" v-if="scope.row.pub == '0'">否</el-tag>
            </template>
        </el-table-column>

        <el-table-column prop="authority" label="权限标识" :show-overflow-tooltip="true"></el-table-column>

        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template #="scope">
            <el-button size="small"  v-if="ckeckCommPermission('addBtn',scope.row)" link type="primary" icon="el-icon-plus" @click="addOrUpdateHandle(false, scope.row.id)" >新增</el-button>
            <el-button size="small"  v-if="ckeckCommPermission('editBtn',scope.row)"  link type="primary" icon="el-icon-edit" @click="addOrUpdateHandle(true, scope.row.id)" >修改</el-button>
            <el-button size="small"   v-if="ckeckCommPermission('delBtn',scope.row)"  link type="primary" icon="el-icon-delete" @click="handleDel(scope.row)">删除</el-button>
          </template>
        </el-table-column>

      </el-table>

      <table-form v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getList"></table-form>
    </div>
  </basic-container>
</template>

<script>
import {fetchMenuTree,fetchMenuTreeByMng} from "@/framework-admin/api/admin/menu";
import {delCore,getCore} from "@/framework-core/api/core/api";
import TableForm from "./menu-form.vue";

export default {
  name: "Menu",
  components: {TableForm},
  data() {
    return {
      pageConfig:{"url":"sys/menu"},
      addOrUpdateVisible: false,
      // 遮罩层
      loading: true,
      // 菜单表格树数据
      menuList: [],

      // 查询参数
      queryParams: {
        parentId: undefined,
      }
    };
  },
  created() {
    this.getList();
  },
  computed: {
    ...mapState({
      permissions: (state) =>state.user.permission,
    })
  },
  methods: {

    ckeckCommPermission:function(key, row) {
      let permissionBtn = true;
      let permissionRow = true;
      if(row){
        permissionRow = row.canDo
      }
      switch (key) {
        case "addBtn":permissionBtn = this.permissions["sys_menu_save_post"]; break;
        case "editBtn":permissionBtn = permissionRow && this.permissions["sys_menu_save_post"]; break;
        case "delBtn": permissionBtn = permissionRow && this.permissions["sys_menu_delete_post"]; break;
      }
      return permissionBtn;
    },
    getList() {
      this.loading = true;
      fetchMenuTreeByMng(this.queryParams).then(response => {
        this.menuList = response.data.data.data;
        this.loading = false;
      });
    },

    addOrUpdateHandle(isEdit, id) {
      this.addOrUpdateVisible = true;
          if(id){
            getCore(this.pageConfig.url,id).then(response => {
              this.$nextTick(() => {
                this.$refs.addOrUpdate.init(isEdit, response.data.data.data);
              });

            });
          }else{
            this.$nextTick(() => {
              this.$refs.addOrUpdate.init(isEdit,{});
            });
          }

    },
    handleDel(row) {
      var $that = this;
      this.$confirm('是否确认删除名称为"' + row.name + '"的数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(function() {
          return delCore(  $that.pageConfig.url,row);
        })
        .then(() => {
          this.getList();
          this.$message.success("删除成功");
        });
    }
  }
};
</script>
